<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Postcode extends ActiveRecord
{
    public static function db() {
        return Yii::$app->db;
    }
    
    public static function tableName() {
        return '{{%postcode}}';
    }
    /**
     * 添加验证码
     * @param string $mobile
     * @param int $code
     */
    public function insertPostcode($data) {
        return self::db()->createCommand()
            ->insert(self::tableName(), [
                'mobile'   => $data['mobile'],
                'code'     => $data['code'],
                'datetime' => $data['datetime']
            ])->execute();
    }
    /**
     * 获取验证码
     * @param unknown $mobile
     */
    public function getPostcode($mobile) {
        $sql  = 'SELECT code, datetime FROM ' . self::tableName() . ' WHERE mobile = ' . $mobile;
        return self::db()->createCommand($sql)->queryOne();
    }
    /**
     * 更改已使用验证码状态
     * @param unknown $mobile
     * @return number
     */
    public function updateStatus($mobile) {
        return self::db()->createCommand()
            ->update(self::tableName(), [
                'status' => 1,
            ],
            ['mobile' => $mobile]
            )->execute();
    }
}
